//#include "itembase.h"
#include "commonfun.h"


namespace CommonFun
{
    bool judgeSharedPtrSuccess(std::shared_ptr<RwXml> sharedPtrXml)
    {
        if(sharedPtrXml && nullptr != sharedPtrXml->getDoc())
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    bool judgeTiXmlNodeSuccess(TiXmlNode *pNode, const char *pName)
    {
        if(nullptr != pNode && nullptr != pName && nullptr != pNode->ToElement() && nullptr != pNode->ToElement()->Attribute(pName))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    //两个元素类型的比较方法
//    bool compareItemBase(ItemBase *pItemLeft, ItemBase *pItemRight)
//    {
//        //根据两个元素的优先级进行比较
//        return pItemLeft->getPriority() < pItemRight->getPriority();
//    }

    std::string getAttribute(TiXmlNode *pNode, const std::string &strName)
    {
        if(judgeTiXmlNodeSuccess(pNode, strName.c_str()))
        {
            return pNode->ToElement()->Attribute(strName.c_str());
        }
        else
        {
            return "";
        }
    }

    TiXmlNode *getChildNode(TiXmlNode *parentNode, const std::string &strName)
    {
        if(nullptr == parentNode)
        {
            return nullptr;
        }

        if(!judgeSharedPtrSuccess(GlobalRwXml::getRwxml()))
        {
            return nullptr;
        }

        return GlobalRwXml::getRwxml()->selectSingleNode(strName, parentNode);
    }

    TiXmlElement *getChildElement(TiXmlNode *parentNode, const std::string &strName)
    {
        if(nullptr != getChildNode(parentNode, strName))
        {
            return getChildNode(parentNode, strName)->ToElement();
        }

        return nullptr;
    }

    TiXmlNode *getNode(const std::string &strName)
    {
        if(!judgeSharedPtrSuccess(GlobalRwXml::getRwxml()))
        {
            return nullptr;
        }

        return GlobalRwXml::getRwxml()->selectSingleNode(strName);
    }

    void delNode(TiXmlNode *pNode)
    {
        if(judgeSharedPtrSuccess(GlobalRwXml::getRwxml()))
        {
            GlobalRwXml::getRwxml()->delSingleNode(pNode);
        }
    }

    TiXmlNode *insertBeforeChild(TiXmlNode *pParentNode, TiXmlNode *pBeforeNode, TiXmlNode *pAddNode)
    {
        TiXmlNode *pAddThis = pParentNode->InsertBeforeChild(pBeforeNode, *pAddNode);
        delete pAddNode;
        return pAddThis;
    }
}
